// pages/login/index.js
import api from '../../request/api.js'
import Toast from '../../miniprogram_npm/@vant/weapp/toast/toast';

Page({
    /**
     * 页面的初始数据
     */
    data: {
        phone: '',
        password: '',
        canIUse: wx.canIUse('button.open-type.getUserInfo'),
        pwdShow: false,
        type: 'password'
    },
    bindGetUserInfo: function (e) {
        console.log(e.detail.userInfo)
    },
  toggleShow() {
    let {pwdShow, type} = this.data
    this.setData({
      pwdShow: !pwdShow,
      type: type==='password' ? 'text' : 'password'
    })
  
  },
  postLogin(){
    this.formSubmit()
  },
  async formSubmit() {
        let {phone, password} = this.data
        console.log(phone)
        let rightPhone = /^1\d{10}$/.test(phone)
        // if(!rightPhone) {Toast('手机号码不正确');return;}
        if(!password) {Toast('请输入密码');return;}
        const res = await api('POST', 'Recycle/user/login', {
          phone,
          password
        })
        console.error(res);
        if (res.data.code === 200) {
          wx.switchTab({
            url: '../index/index',
          })
          wx.setStorageSync('token', res.data.data.token)
            wx.setStorageSync('userInfo', res.data.data)
          } else if (res.data.code === 201) {
            wx.redirectTo({
              url: '../ImproveInformation/index?user_id='+res.data.data,
            })
          } else {
            Toast(res.data.msg)
          }
    },
    bindPhoneInput(e){
        console.error(e)
    },
    bindKeyInput(e){
      let key = e.currentTarget.dataset.name
      console.error(key)
      this.setData({
        [key]: e.detail.value
      })
    },
    /**
     * 生命周期函数--监听页面加载
     */
    onLoad: function (options) {
        let needInfo = wx.getStorageSync('needInfo')
        if (needInfo) {
            wx.redirectTo({
                url: '../ImproveInformation/index',
            })
        } else {
            let token = wx.getStorageSync('token')
            if (token) {
                wx.switchTab({
                    url: '../index/index',
                })
            }
        }
        // 查看是否授权
        wx.getSetting({
            success: function (res) {
                if (res.authSetting['scope.userInfo']) {
                    // 已经授权，可以直接调用 getUserInfo 获取头像昵称
                    wx.getUserInfo({
                        success: function (res) {
                            console.log(res.userInfo)
                        }
                    })
                }
            }
        })
    },

    wxlogin() {
        wx.login({
            success: function (e) {
              console.error(e);
                if (e.code) {
                    //发起网络请求
                    wx.request({
                      url: 'https://feishoukeji.com:8080/Recycle/user/weChatLogin?code='+e.code,
                      data : {
                          code : e.code
                      },
                      method: 'POST',
                      header: {
                        'content-type': 'application/json' // 默认值
                      },
                      success (res) {
                          console.error(res.data);
                          if(res.data.code == 200){
                            wx.switchTab({
                                url: '../index/index',
                            })
                            wx.setStorageSync('token', res.data.data.token)
                            wx.setStorageSync('userInfo', res.data.data)
                          }else if(res.data.code == 201){
                            // wx.setStorageSync('token', res.data.data)
                            wx.navigateTo({
                              url: '../bindPhone/index?user_id='+res.data.data,
                            })
                          }else{
                            Toast(res.data.msg)
                          }
                      }
                    })
                } else {
                    console.log('获取用户登录态失败！' + res.errMsg)
                }
            }
        });
    },
    /**
     * 生命周期函数--监听页面初次渲染完成
     */
    onReady: function () {

    },

    /**
     * 生命周期函数--监听页面显示
     */
    onShow: function () {

    },

    /**
     * 生命周期函数--监听页面隐藏
     */
    onHide: function () {

    },

    /**
     * 生命周期函数--监听页面卸载
     */
    onUnload: function () {

    },

    /**
     * 页面相关事件处理函数--监听用户下拉动作
     */
    onPullDownRefresh: function () {

    },

    /**
     * 页面上拉触底事件的处理函数
     */
    onReachBottom: function () {

    },

    /**
     * 用户点击右上角分享
     */
    onShareAppMessage: function () {

    }
})